<?php
    include('../init.php');
    
    foreach($_POST as $k=>$v){
        $$k = $v;
    }
    
    $q_email = $db->queryUniqueValue("SELECT m_email FROM hs_member WHERE m_email LIKE '$email'");
    
    if($q_email!=NULL){
        echo $status_msg['email_used'];
    }else{
        $time_ = time();
        $seccode = md5($time_);
        $password = substr(str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ23456789'),0,5);
        $password2 = md5($password);
        $sql = <<<sql
            INSERT INTO hs_member(m_name,m_lastname,m_company,m_position,m_email,m_phone,m_url,m_address,m_city,m_postcode,m_state,m_country,m_knowhow,m_password,m_seccode,m_level,m_regis,m_update,m_enews) VALUES(
                '$f_name',
                '$l_name',
                '$c_name',
                '$position',
                '$email',
                '$phone',
                '$url',
                '$address',
                '$city',
                '$post_code',
                '$state',
                '$country',
                '$know',
                '$password2',
                '$seccode',
                '1',
                $time_,
                '0',
                $newsletter
            )
sql;
        $result = $db->query($sql);
        
        if($result){
            $file = APPPATH.'/mail/member_register.txt';
            $handle = fopen($file,'r');
            //$body = fgets($handle);
            $body = '';
            if ($handle) {
                while (!feof($handle)) {
                    $file_ = fgets($handle, 4096);
                    $body .= $file_."<br>";
                }
                fclose($handle);
            }
            
            $mail = new PHPMailer();
            $mail->IsMail();
            $mail->CharSet="utf-8";
            $mail->IsHTML(true);
            $mail->From = MAIN_EMAIL;
            
            $body = str_replace('{NAME}',$f_name,$body);
            $body = str_replace('{LASTNAME}',$l_name,$body);
            $body = str_replace('{SITENAME}',SITENAME,$body);
            $body = str_replace('{SECCODE}',$seccode,$body);
            $body = str_replace('{PASSWORD}',$password,$body);
            $body = str_replace('{CONFIRMLINK}',URL."/member_confirm.php?code=$seccode",$body);
            
            $mail->FromName = REGIS_CONFIRM_TITLE;
            $mail->AddReplyTo(MAIN_EMAIL, REGIS_CONFIRM_TITLE);
            $mail->AddAddress($email);
            $mail->Subject = REGIS_CONFIRM_TITLE;
            $mail->Body = $body;
            $mail->Send();
            
            echo 'success';
        }else{
            echo $status_msg['cant_add'];
        }
    }
?>